<?php
/*
// @author : lael
// blog : http://hi.baidu.com/lael80
// http://www.gzyd.net
// all right reserved.
*/
namespace ydsdk\models;
class stats extends \ydsdk\core\common {
	public function __construct(){
		parent::__construct();
	}
	
	 /**
	 * check one day data exist or not   
	 * 
	 * @access  public
	 * @return  row_array,
	 */      
	public function check_today($class_id=0,$school_id=0)
	{
		$flag = FALSE;
		$sql = 'select id from ##deerxi_class_stats where TO_DAYS(from_unixtime(cdate)) = TO_DAYS(NOW()) and class_id = '.$class_id.' and school_id='.$school_id.'';
		$res = $this->db->get_row($sql);
		if($res)
		{
			$flag = TRUE;
		}
		return $flag;
	}
	
	public function leave_stats($starttime,$endtimes,$cid,$sid)
	{
		$date=floor(($endtimes-$starttime)/86400);
		for($i=0;$i<=$date;$i++)
		{
			if($i>0&&$i<$date)
			{
				$starttime = $starttime+86400;
			}
			if($i==$date)
			{
				$starttime = $endtimes;
			}
		   // $result = $this->check_today($cid,$sid);
		    $sql = 'select id from ##deerxi_class_stats where TO_DAYS(from_unixtime(cdate)) = TO_DAYS(from_unixtime('.$starttime.')) and class_id = '.$cid.' and school_id='.$sid.'';
		    $res = $this->db->get_row($sql);
		    if($res)
			{
				$sql  ='update ##deerxi_class_stats set l_counter=l_counter+1 where class_id = '.$cid.' and school_id='.$sid.' and TO_DAYS(from_unixtime(cdate)) = TO_DAYS(from_unixtime('.$starttime.'))';
				$this->db->query($sql);
			}
			else
			{
				$sql = 'insert into ##deerxi_class_stats(school_id,class_id,l_counter,cdate)values('.$sid.','.$cid.',1,'.$starttime.')';
				$this->db->query($sql);
			}
		}
	}
	//入园或出园标识统计
	public function class_stats($class_id=0,$school_id=0,$iccode=0)
	{
		$sql = 'SELECT count(1) as counter FROM ##deerxi_shuttle where iccode = \''.$iccode.'\' and TO_DAYS(from_unixtime(add_time)) = TO_DAYS(from_unixtime('.time().'))';
		$res = $this->db->get_row($sql);
		$counter = 0;
		if($res)
		{
			$counter = $res['counter'];
		}
		$sql = '';
		if(!$this->check_today($class_id,$school_id))
		{
			$sql = 'insert into ##deerxi_class_stats (school_id,class_id,i_counter,o_counter,l_counter,cdate)values('.$school_id.','.$class_id.',1,0,0,'.time().')';
			$this->db->query($sql);
		} 
	   if($counter%2==0)
		{
			 $sql  ='update ##deerxi_class_stats set  o_counter=o_counter+1 where class_id = '.$class_id.' and school_id='.$school_id.' and TO_DAYS(from_unixtime(cdate)) = TO_DAYS(from_unixtime('.time().'))';
		 }
		 else{
			 $sql = 'update ##deerxi_class_stats set i_counter = i_counter+1 where class_id = '.$class_id.' and school_id='.$school_id.' and TO_DAYS(from_unixtime(cdate)) = TO_DAYS(from_unixtime('.time().'))';
		 }  
		$this->db->query($sql);
	}	
	//入园或出园标识统计根据时间范围
	public function class_stats2($class_id=0,$school_id=0,$iccode=0)
	{
		$sql = 'SELECT count(1) as counter FROM ##deerxi_shuttle where iccode = \''.$iccode.
		      '\' and TO_DAYS(from_unixtime(add_time)) = TO_DAYS(from_unixtime('.time().')) and add_time>='.(time()-150).' and add_time<='.time();
		$res = $this->db->get_row($sql);
		$counter = 0;
		if($res)
		{
			$counter = $res['counter'];
		}
		if ($counter==1){	
			$sql = '';
			if(!$this->check_today($class_id,$school_id))
			{
				$sql = 'insert into ##deerxi_class_stats (school_id,class_id,i_counter,o_counter,l_counter,cdate)values('.$school_id.','.$class_id.',0,0,0,'.time().')';
				$this->db->query($sql);
			}
			
			if(date('H',time())>='11')//11点之后全部算出校
			{
				//var_dump('out');
				$sql  ='update ##deerxi_class_stats set  o_counter=o_counter+1 where class_id = '.$class_id.' and school_id='.$school_id.' and TO_DAYS(from_unixtime(cdate)) = TO_DAYS(from_unixtime('.time().'))';
			}
			else{
				//var_dump('in');
				$sql = 'update ##deerxi_class_stats set i_counter = i_counter+1 where class_id = '.$class_id.' and school_id='.$school_id.' and TO_DAYS(from_unixtime(cdate)) = TO_DAYS(from_unixtime('.time().'))';
			}
			$this->db->query($sql);
		}
	}	
}